/*
 * This source code is part of TWaver 4.0
 *
 * Serva Software PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * Copyright 2002 - 2011 Serva Software. All rights reserved.
 */

package demo.tree;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.util.Enumeration;

import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.SwingUtilities;

import twaver.Element;
import twaver.PopupMenuGenerator;
import twaver.TDataBox;
import twaver.TView;
import twaver.swing.TPopupMenu;
import twaver.tree.ElementNode;
import twaver.tree.TTree;

public class TreePopupMenuGenerator implements PopupMenuGenerator {
	private TTree tree;
	
	public TreePopupMenuGenerator(TTree tree){
		this.tree = tree;
	}
	
	public JPopupMenu generate(TView tview, MouseEvent mouseEvent){
    	JPopupMenu popMenu = new TPopupMenu("TWaver", Color.ORANGE, Color.WHITE, Color.BLUE, 20);
    	JMenuItem item = null;
    	final TDataBox box = tview.getDataBox();
    	
    	if(tree.getRootNode() instanceof ElementNode){
    		item = new JMenuItem("Reset Root Node");
            item.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                	tree.setRootElement(null);
                }
            });
            popMenu.add(item);
    	}
    	
    	if(box.getSelectionModel().size() == 1){
			final Element element = box.getLastSelectedElement();		
			
			// set as root node
    		item = new JMenuItem("Set As Root Node");
            item.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                	tree.setRootElement(element);
                }
            });
            popMenu.add(item);
			
            popMenu.addSeparator();
            
            // expand element
            item = new JMenuItem("Expand");
            item.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                	tree.expandAll(element);
                }
            });
            popMenu.add(item);
            
            // collapse element
            item = new JMenuItem("Collapse");
            item.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                	tree.collapseAll(element);
                }
            });
            popMenu.add(item);
    	}
        
        // expand all
        item = new JMenuItem("Expand All");
        item.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
            	tree.expandAll();
            }
        });
        popMenu.add(item);   
        
        // collapse element
        item = new JMenuItem("Collapse All");
        item.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            	tree.collapseAll();
            }
        });
        popMenu.add(item);
        popMenu.addSeparator();
        
    	if(box.getSelectionModel().size() > 0){
    		JMenuItem moveTop = new JMenuItem("Move Top");
    		JMenuItem moveUp = new JMenuItem("Move Up");
    		JMenuItem moveDown = new JMenuItem("Move Down");
    		JMenuItem moveBottom = new JMenuItem("Move Bottom");
    		popMenu.add(moveTop);
    		popMenu.add(moveUp);
    		popMenu.add(moveDown);
    		popMenu.add(moveBottom);
    		popMenu.addSeparator();
    		
    		moveTop.addActionListener(new ActionListener(){
    			public void actionPerformed(ActionEvent e) {
    				box.moveSelectionToTop();
    			}
    		});
    		moveUp.addActionListener(new ActionListener(){
    			public void actionPerformed(ActionEvent e) {
    				box.moveSelectionToUp();
    			}
    		});
    		moveDown.addActionListener(new ActionListener(){
    			public void actionPerformed(ActionEvent e) {
    				box.moveSelectionToDown();
    			}
    		});
    		moveBottom.addActionListener(new ActionListener(){
    			public void actionPerformed(ActionEvent e) {
    				box.moveSelectionToBottom();
    			}
    		});
    	}
        
        item = new JMenuItem("Breadth First Enumeration");
        item.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
            	Thread t = new Thread(new Runnable(){
					public void run() {
		        		final Enumeration e = box.breadthFirstEnumeration();
		        		while(e.hasMoreElements()){
		        			try {
								SwingUtilities.invokeAndWait(new Runnable(){
									public void run() {
										Element element = (Element)e.nextElement();
										box.getSelectionModel().setSelection(element);
									}
								});
							} catch (Exception ex) {
							}
							try {
								Thread.sleep(100);
							} catch (Exception ex) {
							}
		        		}
					}
            	});
            	t.start();
            }
        });
        popMenu.add(item);
        
        item = new JMenuItem("Depth First Enumeration");
        item.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
            	Thread t = new Thread(new Runnable(){
					public void run() {
		        		final Enumeration e = box.depthFirstEnumeration();
		        		while(e.hasMoreElements()){
		        			try {
								SwingUtilities.invokeAndWait(new Runnable(){
									public void run() {
										Element element = (Element)e.nextElement();
										box.getSelectionModel().setSelection(element);
									}
								});
							} catch (Exception ex) {
							}
							try {
								Thread.sleep(100);
							} catch (Exception ex) {
							}
		        		}
					}
            	});
            	t.start();
            }
        });
        popMenu.add(item);
        
        popMenu.addSeparator();  
        
        // show/hide root node
        if(tree.isRootVisible()){	
            item = new JMenuItem("Hide Root Node");
        }else{
        	item = new JMenuItem("Show Root Node");
        }
        item.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            	tree.setRootVisible(!tree.isRootVisible());
            }
        });
        popMenu.add(item);

        if (tree.getDataBox().getSelectionModel().size() > 0) {
            item = new JMenuItem("Delete");
            item.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                	tree.getDataBox().removeSelectedElements();
                }
            });
            popMenu.add(item);
        }
        
        return popMenu;
    }
    

}